package demo;
//数组整个元素向后移可以用arraycopy
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

public class Test2 {
    /**
     * 遍历元素
     * 1、 System.out.println(arrayList);
     * 2、for循环
     * 3、foreach - 增强for循环
     * 4、迭代器1
     * 5、迭代器2
     */
    public static void main(String[] args) {
        ArrayList<Integer> arrayList = new ArrayList<>();
        arrayList.add(1);
        arrayList.add(2);
        arrayList.add(3);
        arrayList.add(5);
        arrayList.add(6);
        System.out.println(arrayList);
        System.out.println("============================");
        int size = arrayList.size();
        for (int i = 0; i < size; i++) {
            System.out.print(arrayList.get(i)+" ");
        }
        System.out.println();
        System.out.println("============================");
        for (Integer x:arrayList) {
            System.out.print(x+" ");
        }
        System.out.println();
        System.out.println("============================");
        Iterator it = arrayList.iterator();
        while(it.hasNext()){
            System.out.print(it.next()+" ");
        }
        System.out.println();
        System.out.println("============================");
       // Iterator li = arrayList.listIterator();
        ListIterator li = arrayList.listIterator();
        while(li.hasNext()){
            System.out.print(li.next()+" ");
        }
        System.out.println();

    }
    public static void main1(String[] args) {
        ArrayList<Integer> arrayList = new ArrayList<>();
        arrayList.add(1);
        arrayList.add(2);
        arrayList.add(3);
        arrayList.add(5);
        arrayList.add(6);

        arrayList.add(3,4);
        System.out.println(arrayList);
        System.out.println("==============================");
        arrayList.remove(new Integer(3));
        System.out.println(arrayList);
        arrayList.remove(2);
        System.out.println(arrayList);
        System.out.println("==============================");
        Integer ret =  arrayList.get(3);
        System.out.println(ret);
        arrayList.set(3,8);
        System.out.println(arrayList);
        System.out.println( arrayList.contains(new Integer(2)));
        System.out.println("==============================");
        Integer index = arrayList.indexOf(new Integer(8));
        System.out.println(index);
        Integer in = arrayList.lastIndexOf(new Integer(2));
        System.out.println(in);
        System.out.println("===============================");
        List<Integer> list = arrayList.subList(1,3);
        System.out.println(list);
        list.set(1,99);
        System.out.println(list);
        System.out.println(arrayList);
    }

}
